package com.example.mntz.controller;

import com.example.mntz.model.LayuiTable;
import com.example.mntz.model.User;
import com.example.mntz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService service;

    @RequestMapping("/adminUserHtml")
    public String adminUserHtml(){
        return "admin-userlist";
    }
    @ResponseBody
    @RequestMapping("/userlist")
    public LayuiTable userlist(Integer limit , Integer page, User user){
        return service.selectUserLists(limit,page,user);
    }

    @RequestMapping("/adminIUuser")
    public ModelAndView adminIUuser(Integer uId){
        ModelAndView modelAndView = new ModelAndView("admin-user");
        User u = new User();
        String a = "添加用户";
        if(uId != null){
            u= service.selectUserById(uId);
            a = "修改用户";
        }
        modelAndView.addObject("legend",a);
        modelAndView.addObject("user",u);
        return modelAndView;
    }

    @ResponseBody
    @PostMapping("/insertUser")
    public void insertUser(User user){
        service.insertUser(user);
    }
    @ResponseBody
    @DeleteMapping("/deleteUsers")
    public void deleteUsers(@RequestParam("uIds[]")List<Integer> uIds){
        service.deleteUsers(uIds);
    }
    @RequestMapping("/sysadmin")
    public String sysadmin(){
        return "admin-sys";
    }
}
